<?php
require_once(ROOTDIR."/include/database/class.ServerQueries.php");
require_once(ROOTDIR."/include/database/class.ReasonQueries.php");
require_once(ROOTDIR."/include/class.rcon.php");

$serverId = $_GET['serverId'];

$serverQueries = new ServerQueries();
$reasonQueries = new ReasonQueries();

$server = $serverQueries->getServer($serverId);
$reasons = $reasonQueries->getReasonList();

// Create an rcon object to connect to a single server on each iteration
$r = new rcon($server->getIp(),$server->getPort(),$server->getRcon());

$active = 0;
// Make sure it connected to the server
if($r->isValid()) {
  $active = 1;
  $r->Auth(); // Establish the connection
  // First delete the existing clanMembers keygroup and then re-create it
  $r->sendRconCommand("es_keygroupdelete GlobalBan_Reason; es_keygroupcreate GlobalBan_Reason");

  // Cycle through each user and create the keygroup
  foreach($reasons as $reason) {

    // Create a key for the user
    $command = "gb_addReason ".$reason->getId()." \"".$reason->getReason()."\"";
    $r->sendRconCommand($command);
  }

  // Now save the keygroup
  $r->sendRconCommand("es_keygroupsave GlobalBan_Reason |GlobalBan");

  // Now reload the clan_db script
  $r->sendRconCommand("es_reload GlobalBan");
}

// Send back the new active state
header('Content-Type: text/xml');
header("Cache-Control: no-cache, must-revalidate");

function convertXmlSpecial($string) {
  $string = str_replace("&", "&amp;", $string);
  $string = str_replace("'", "&apos;", $string);
  $string = str_replace("\"", "&quot;", $string);
  $string = str_replace("<", "&lt;", $string);
  $string = str_replace(">", "&gt;", $string);
  return $string;
}
?>
<?echo "<?xml version=\"1.0\" ?>";?>
<root>
  <id><?=$server->getId()?></id>
	<name><?=convertXmlSpecial($server->getName())?></name>
	<active><?=$active?></active>
</root>
